#!/bin/sh

if [ "$(uci -q get network.omrvpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		delete network.glorytun
		delete network.omrvpn
		set network.omrvpn=interface
		set network.omrvpn.ifname=tun0
		set network.omrvpn.proto=dhcp
		set network.omrvpn.ip4table=vpn
		set network.omrvpn.multipath=off
		set network.omrvpn.leasetime=12h
		set network.omrvpn.type=tunnel
		set network.omrvpn.txqueuelen=100
		set network.omrvpn.metric=1200
		commit network
	EOF
fi

if [ "$(uci -q get glorytun.vpn.localip)" = "10.0.0.2" ]; then
	uci -q batch <<-EOF >/dev/null
		delete glorytun.vpn.localip
		delete glorytun.vpn.remoteip
	EOF
fi

if [ "$(uci -q get network.omrvpn.txqueuelen)" = "1000" ]; then
	uci -q batch <<-EOF >/dev/null
		set network.omrvpn.txqueuelen=100
		commit network
	EOF
fi

if [ "$(uci -q get openvpn.omr.proto)" != "tcp-client" ]; then
	uci -q batch <<-EOF >/dev/null
		set openvpn.omr=openvpn
		set openvpn.omr.dev=tun0
		set openvpn.omr.port=65301
		set openvpn.omr.cipher=AES-256-CBC
		set openvpn.omr.proto=tcp-client
		set openvpn.omr.enabled=0
		set openvpn.omr.ncp_disable=1
		set openvpn.omr.auth_nocache=1
		set openvpn.omr.client=1
		set openvpn.omr.tls_client=1
		set openvpn.omr.reneg_sec=0
		set openvpn.omr.allow_recursive_routing=1
		set openvpn.omr.sndbuf=0
		set openvpn.omr.rcvbuf=0
		set openvpn.omr.route_delay=5
		commit openvpn
	EOF
fi

uci -q delete openvpn.omr.secret

#if [ "$(uci -q get openvpn.omr.com_lzo)" = "" ]; then
#	uci -q batch <<-EOF >/dev/null
#		set openvpn.omr.comp_lzo=adaptive
#	EOF
#fi
if [ "$(uci -q get ipsec.ipsec.password)" = "myvpn" ]; then
	uci -q batch <<-EOF >/dev/null
		set ipsec.ipsec.enabled='0'
		commit ipsec
	EOF
	chmod u+x /etc/init.d/ipsec
fi

if [ "$(uci -q get dsvpn.vpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set dsvpn.vpn=dsvpn
		set dsvpn.vpn.dev=tun0
		set dsvpn.vpn.port=65011
		set dsvpn.vpn.localip=10.255.251.2
		set dsvpn.vpn.remoteip=10.255.251.1
		set dsvpn.vpn.mode=client
		set dsvpn.vpn.enable=0
		commit dsvpn
	EOF
fi

if [ "$(uci -q get dsvpn.vpn.mode)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set dsvpn.vpn.mode=client
		commit dsvpn
	EOF
fi

if [ "$(uci -q show firewall | grep omrvpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list firewall.zone_vpn.network=omrvpn
		delete firewall.allow_dhcp_request_vpn
		set firewall.allow_dhcp_request_vpn=rule
		set firewall.allow_dhcp_request_vpn.name=Allow-DHCP-Request-VPN
		set firewall.allow_dhcp_request_vpn.src=vpn
		set firewall.allow_dhcp_request_vpn.proto=udp
		set firewall.allow_dhcp_request_vpn.dest_port=67
		set firewall.allow_dhcp_request_vpn.target=ACCEPT
		set firewall.allow_dhcp_request_vpn.family=ipv4
		commit firewall
	EOF
fi
if [ "$(uci -q get ucitrack.@network[-1].affects | grep glorytun)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@network[-1].affects="glorytun"
		commit ucitrack
	EOF
fi
if [ "$(uci -q get ucitrack.@network[-1].affects | grep glorytun-udp)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@network[-1].affects="glorytun-udp"
		commit ucitrack
	EOF
fi
if [ "$(uci -q get ucitrack.@network[-1].affects | grep dsvpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@network[-1].affects="dsvpn"
		commit ucitrack
	EOF
fi
if [ "$(uci -q get ucitrack.@glorytun[-1].affects | grep openmptcprouter-vps)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@glorytun[-1].affects="openmptcprouter-vps"
		commit ucitrack
	EOF
fi
if [ "$(uci -q get ucitrack.@glorytun-udp[-1].affects | grep openmptcprouter-vps)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@glorytun-udp[-1].affects="openmptcprouter-vps"
		commit ucitrack
	EOF
fi

if [ "$(uci -q get glorytun-udp.vpn.key)" = "" ] && [ "$(uci -q get glorytun.vpn.key)" != "" ]; then
	uci -q batch <<-EOF >/dev/null
		set glorytun-udp.vpn.key=$(uci -q get glorytun.vpn.key)
		set glorytun-udp.vpn.host=$(uci -q get glorytun.vpn.host)
	EOF
	if [ "$(uci -q get glorytun-udp.vpn.proto)" = "udp" ]; then
		uci -q batch <<-EOF >/dev/null
			set glorytun-udp.vpn.enable=1
			set glorytun.vpn.enable=0
			commit glorytun
		EOF
	fi
	uci -q commit glorytun-udp
fi



rm -f /tmp/luci-indexcache
exit 0
